#include "Events.h"

using namespace Utils;

AppLoopEvent::AppLoopEvent()
{
}

AppLoopEvent::AppLoopEvent(boost::function<void ()> function): function(function)
{
	AppLoopEventsQueue::getInstance()->pushEvent(this);
}

void AppLoopEvent::operator() ()
{
	if (function)
	{
		function();
	}
	else
	{
#ifdef _DEBUG
		std::clog << __FUNCTION__ << std::endl;
#endif
	}
}

AppLoopEventsQueue::AppLoopEventsQueue()
{
}

AppLoopEventsQueue* AppLoopEventsQueue::instance = NULL;

AppLoopEventsQueue* AppLoopEventsQueue::getInstance()
{
	if (instance == NULL)
		instance = new AppLoopEventsQueue();
	return instance;
}

bool AppLoopEventsQueue::getEvent(AppLoopEvent& ev)
{
	if (!queue.empty())
	{
		ev = queue.front();
		queue.pop();
		return true;
	}
	return false;
}

void AppLoopEventsQueue::pushEvent(AppLoopEvent* ev)
{
	AppLoopEvent tmp(*ev);
	queue.push(tmp);
}

